package com.wanlian.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.AuthorityDao;
import com.wanlian.entity.Authority;
import com.wanlian.model.ResultModel;
import org.springframework.stereotype.Component;


@Component
public class AuthorityDaoImpl extends GenericDaoImpl<Authority> implements AuthorityDao {
    public AuthorityDaoImpl() {
        super(Authority.class);
    }

    @Override
    public ResultModel queryPageList(PageCondition pageCondition) {
        StringBuffer sqlWhere = new StringBuffer(" where 1=1 and isenable=true ");
        ArrayList<Object> params = new ArrayList<Object>();
        if (pageCondition.isExistByKey("authorityName")) {
            if (pageCondition.getConditions().get("authorityName") != null && pageCondition.getConditions().get("authorityName") != "") {
                sqlWhere.append(" and authorityname like ?");
                params.add("%" + pageCondition.getConditions().get("authorityName") + "%");
            }
        }

        List<Authority> models = this.queryForListByPage("from " + entityClassName() + sqlWhere + " order by authorityid asc", params.toArray(), pageCondition.getPage());
        String hql = String.format("select count(*) from %s " + sqlWhere, entityClassName());
        Long totalSize = this.queryCount(hql, params.toArray());
        ResultModel resultModel = new ResultModel();
        resultModel.setCount(totalSize);
        resultModel.setObject(models);
        return resultModel;
    }

    @Override
    public List<Authority> queryListByParentid(int parentid) {
        return this.queryForListByWhere("   isenable = 1 and parentid = " + parentid, null);
    }

    @Override
    public Authority queryByName(String name,String methodorUrl) {
        String hql = String.format("from %s where (authorityname=? or methodorurl=?) and isenable=1 ", entityClassName());
        Authority authority = this.queryForObject(hql, new Object[]{name,methodorUrl});
        return authority;
    }

    @Override
    public Authority queryByMethodUrl(String methodorUrl) {
        String hql = String.format("from %s where  methodorurl=? and isenable=1 ", entityClassName());
        Authority authority = this.queryForObject(hql, new Object[]{methodorUrl});
        return authority;
    }
}
